package com.corpgovernment.organization.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.corpgovernment.api.organization.model.commonInvoice.MbCommonInvoiceInfoRequestVo;
import com.corpgovernment.api.organization.model.commonInvoice.MbCommonInvoiceInfoResponseVo;
import com.corpgovernment.api.organization.model.commonInvoice.request.InvoiceListAllTypeRequest;
import com.corpgovernment.common.base.AbstractBaseService;
import com.corpgovernment.organization.convert.IMbCommonInvoiceInfoMapper;
import com.corpgovernment.organization.entity.db.MbCommonInvoiceInfo;
import com.corpgovernment.organization.mapper.MbCommonInvoiceInfoMapper;
import com.corpgovernment.organization.vo.MbCommonInvoiceInfoPageVo;
import com.corpgovernment.organization.vo.MbCommonInvoiceInfoPageVo.MbCommonInvoiceInfoVo;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.ctrip.corp.obt.shard.context.PageContext;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: CommonInvoiceInfoService
 * @description: TODO
 * @author: ezchen
 * @date: Created in 10:49 2019/8/13
 * @Version: 1.0
 **/
@Slf4j
@Service
public class CommonInvoiceInfoService extends AbstractBaseService {

    @Autowired
    private MbCommonInvoiceInfoMapper mbCommonInvoiceInfoMapper;


    /**
     * 分页查询发票信息
     *
     * @param request
     * @return
     */
    public MbCommonInvoiceInfoPageVo listByPage(InvoiceListAllTypeRequest request) {
        // 设置分页
        PageContext.startPage(request.getPageNum(), request.getPageSize());
        List<MbCommonInvoiceInfo> mbCommonInvoiceInfoEntityList = mbCommonInvoiceInfoMapper.list(request.getInvoiceTitle(), request.getUid(), request.getOrgId(), request.getCorpId(), request.getType());
        PageInfo<MbCommonInvoiceInfo> pageInfo = new PageInfo<>(mbCommonInvoiceInfoEntityList);
        // Response
        MbCommonInvoiceInfoPageVo responseVo = new MbCommonInvoiceInfoPageVo();
        //responseVo.setCorpList(copyList(this.listByOrgId(mbCommonInvoiceInfoRequestVo.getOrgId()), CommonInvoiceVo.class));
        if (!CollectionUtils.isEmpty(pageInfo.getList())) {
            List<MbCommonInvoiceInfo> mbCommonInvoiceInfoList = pageInfo.getList();
            mbCommonInvoiceInfoList.forEach(bean -> {
                bean.setType(StringUtils.isBlank(bean.getUid()) ? 1 : 2);
            });
            List<MbCommonInvoiceInfoVo> mbCommonInvoiceInfoVoList = mbCommonInvoiceInfoList.stream().map(IMbCommonInvoiceInfoMapper.INSTANCT::po2Vo).collect(Collectors.toList());
            responseVo.setList(mbCommonInvoiceInfoVoList);
        }
        responseVo.setCount(pageInfo.getTotal());
        responseVo.setPageNum(request.getPageNum());
        responseVo.setPageSize(request.getPageSize());
        responseVo.setTotalPage(pageInfo.getPages());
        return responseVo;
    }

    /**
     * 分页查询发票信息 公司和个人
     *
     * @param mbCommonInvoiceInfoRequestVo
     * @return
     */
    public MbCommonInvoiceInfoResponseVo listByOrgAndSelf(MbCommonInvoiceInfoRequestVo mbCommonInvoiceInfoRequestVo) {
        // 设置分页
        PageContext.startPage(mbCommonInvoiceInfoRequestVo.getPageNum(), mbCommonInvoiceInfoRequestVo.getPageSize());
        List<MbCommonInvoiceInfo> mbCommonInvoiceInfoEntityList = mbCommonInvoiceInfoMapper.listOrgAndSef(mbCommonInvoiceInfoRequestVo);
        PageInfo<MbCommonInvoiceInfo> pageInfo = new PageInfo<>(mbCommonInvoiceInfoEntityList);
        // Response
        MbCommonInvoiceInfoResponseVo responseVo = new MbCommonInvoiceInfoResponseVo();
        responseVo.setList(pageInfo.getList());
        responseVo.setCount(pageInfo.getTotal());
        responseVo.setPageNum(mbCommonInvoiceInfoRequestVo.getPageNum());
        responseVo.setPageSize(mbCommonInvoiceInfoRequestVo.getPageSize());
        responseVo.setTotalPage(pageInfo.getPages());
        return responseVo;
    }

    public MbCommonInvoiceInfo getDefaultCommonInvoiceInfo(String uid, String orgId) {

        return mbCommonInvoiceInfoMapper.getLastOne(uid, orgId);
    }

    /**
     * 获取当前组织关联的发票信息
     *
     * @param orgId
     * @return
     */
    public List<MbCommonInvoiceInfo> listByOrgIdReal(String orgId) {
        if (StringUtils.isEmpty(orgId)) {
            return null;
        }

        return mbCommonInvoiceInfoMapper.listByOrgId(orgId);
    }

    /**
     * 获取当前租户下组织关联的发票信息
     */
    public List<MbCommonInvoiceInfo> listAllOrgIdReal(String invoiceTitle) {
        return mbCommonInvoiceInfoMapper.listAllCorpInvoice(invoiceTitle);
    }

    /**
     * 添加发票
     *
     * @param mbCommonInvoiceInfo
     * @return
     */
    public void addCommonInvoiceInfo(MbCommonInvoiceInfo mbCommonInvoiceInfo) {
        mbCommonInvoiceInfoMapper.insertSelective(mbCommonInvoiceInfo);
    }

    public int batchInsertCommonInvoiceInfo(List<MbCommonInvoiceInfo> mbCommonInvoiceInfoList){
        if (CollectionUtils.isEmpty(mbCommonInvoiceInfoList)){
            return 0;
        }
        return mbCommonInvoiceInfoMapper.insertList(mbCommonInvoiceInfoList);
    }

    /**
     * 批量新增或更新发票
     *
     * @param mbCommonInvoiceInfoList
     */
    public void addOrUpdateCommonInvoiceInfo(List<MbCommonInvoiceInfo> mbCommonInvoiceInfoList) {

        if (CollectionUtils.isEmpty(mbCommonInvoiceInfoList)) {
            return;
        }

        List<MbCommonInvoiceInfo> insertList = mbCommonInvoiceInfoList.stream().filter(i -> null == i.getId() || i.getId() == 0).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(insertList)) {
            mbCommonInvoiceInfoMapper.insertSelectiveBatch(insertList);
        }

        List<MbCommonInvoiceInfo> updateList = mbCommonInvoiceInfoList.stream().filter(i -> null != i.getId() && i.getId() > 0).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(updateList)) {
            mbCommonInvoiceInfoMapper.updateSelectiveBatch(updateList);
        }
    }

    /**
     * 修改发票
     *
     * @param mbCommonInvoiceInfo
     * @return
     */
    public boolean updateCommonInvoiceInfo(MbCommonInvoiceInfo mbCommonInvoiceInfo) {

        if (mbCommonInvoiceInfo == null || mbCommonInvoiceInfo.getId() == null) {
            return false;
        }
        return mbCommonInvoiceInfoMapper.updateByPrimaryKeySelective(mbCommonInvoiceInfo) > 0;
    }

    /**
     * 删除发票
     *
     * @param mbCommonInvoiceInfo
     * @return
     */
    public boolean deleteCommonInvoiceInfo(MbCommonInvoiceInfo mbCommonInvoiceInfo) {

        if (mbCommonInvoiceInfo == null || mbCommonInvoiceInfo.getId() == null || StringUtils.isBlank(mbCommonInvoiceInfo.getUid())) {
            return false;
        }
        if (mbCommonInvoiceInfoMapper
            .list(StringUtils.EMPTY, mbCommonInvoiceInfo.getUid(), mbCommonInvoiceInfo.getOrgId(),
                mbCommonInvoiceInfo.getOrgId(), 2)
            .stream()
            .noneMatch(a -> a.getId().equals(mbCommonInvoiceInfo.getId()))) {
            log.info("该数据[{}]不属于当前用户[{}]", mbCommonInvoiceInfo.getId(), mbCommonInvoiceInfo.getUid());
            throw new CorpBusinessException(OrganizationResponseCodeEnum.DATA_OPERATION_NO_PERMISSION);
        }
        MbCommonInvoiceInfo updateParam = new MbCommonInvoiceInfo();
        updateParam.setId(mbCommonInvoiceInfo.getId());
        updateParam.setIsDeleted(true);
        return mbCommonInvoiceInfoMapper.updateByPrimaryKeySelective(updateParam) > 0;
    }

    /**
     * 根据组织ID删除发票
     *
     * @param orgId
     */
    public Integer deleteByOrgId(String orgId) {

        if (StringUtils.isEmpty(orgId)) {
            return null;
        }

        return mbCommonInvoiceInfoMapper.deleteByOrgId(orgId);
    }
}
